package com.fanqj.resilience4j.spring.config;

import io.github.resilience4j.bulkhead.configure.BulkheadConfigurationProperties;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties;
import io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties;
import io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties;
import io.github.resilience4j.retry.configure.RetryConfigurationProperties;
import io.github.resilience4j.timelimiter.configure.TimeLimiterConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

/**
 * 自定义配置resilience4j组件对象
 * @author <a href="mailto:fanqijin@wxchina.com">fanqj</a>
 * @Date 2021年01月27日 14:51
 * @Version 6.4.1
 */
@Component
@ComponentScan(basePackages = {"io.github.resilience4j"})
public class Resilience4jConfig {
    /**
     * 断路器配置属性
     * @return
     */
    @Bean
    public CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties() {
        io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties1 =
                new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties();
        instanceProperties1.setMinimumNumberOfCalls(10);
        io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties2 =
                new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties();
        instanceProperties2.setMinimumNumberOfCalls(20);

        io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties defaultProperties =
                new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties();
        defaultProperties.setSlidingWindowSize(100).setMinimumNumberOfCalls(40).setSlidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED);

        CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
        circuitBreakerConfigurationProperties.setCircuitBreakerAspectOrder(400);
        circuitBreakerConfigurationProperties.getConfigs().put("default", defaultProperties);
        circuitBreakerConfigurationProperties.getInstances().put("backend1", instanceProperties1);
        circuitBreakerConfigurationProperties.getInstances().put("backend2", instanceProperties2);
        return circuitBreakerConfigurationProperties;
    }

    @Bean
    public BulkheadConfigurationProperties bulkheadConfigurationProperties() {
        return new BulkheadConfigurationProperties();
    }

    @Bean
    public ThreadPoolBulkheadConfigurationProperties threadPoolBulkheadConfigurationProperties() {
        return new ThreadPoolBulkheadConfigurationProperties();
    }

    @Bean
    public RateLimiterConfigurationProperties rateLimiterConfigurationProperties() {
        return new RateLimiterConfigurationProperties();
    }

    @Bean
    public RetryConfigurationProperties retryConfigurationProperties() {
        return new RetryConfigurationProperties();
    }

    @Bean
    public TimeLimiterConfigurationProperties timeLimiterConfigurationProperties() {
        return new TimeLimiterConfigurationProperties();
    }

}
